package com.itheima.case2.service.impl;

import com.itheima.case2.dao.UserMapper;
import com.itheima.case2.dao.UserRoleMapper;
import com.itheima.case2.pojo.po.User;
import com.itheima.case2.pojo.vo.AddUser;
import com.itheima.case2.pojo.vo.PageResult;
import com.itheima.case2.pojo.vo.QueryPageBean;
import com.itheima.case2.pojo.vo.UpdateUser;
import com.itheima.case2.service.UserService;
import com.itheima.case2.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserServiceImpl implements UserService {

    @Override
    public PageResult selectUser(QueryPageBean queryPageBean) {
        //实用工具类获得会话对象
        SqlSession session = SqlSessionUtil.getSession();
        //使用会话对象获取接口代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> users = null;
        //判断查找条件类型
        Long total = 0l;
        if (queryPageBean.getQueryParams().containsKey("username")){
            //使用接口代理对象调用方法
            users = mapper.selectUser(queryPageBean);
            total = mapper.findUserTotal(queryPageBean);
        }else {
            users = mapper.selectUser2(queryPageBean);
            total = mapper.findUserTotal2(queryPageBean);
        }
        //结果封装
        PageResult pageResult = new PageResult(total, users);
        //7.释放资源
        session.close();
        return pageResult



    @Overrid

    public void updateUser(UpdateUser updateUser) {
        //使用工具类获得会话对象
        SqlSession session = SqlSessionUtil.getSession();
        //使用会话对象获取接口代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        //调用更新方法
        mapper.updateUserById(updateUser);
        //获取中间表代理对象
        UserRoleMapper userRoleMapper = session.getMapper(UserRoleMapper.class);
        //根据user id删除中间表的信息
        userRoleMapper.deleteByUserId(updateUser.getId());
        //在中间表插入新信息
        List<String> roleIds = updateUser.getRoleIds();
        for (String roleId : roleIds) {
            userRoleMapper.add(updateUser.getId(), Integer.parseInt(roleId));
        }
        //释放资源
        session.close();
    }

    @Override
    public void addUser(AddUser addUser) {
        //使用工具类获得会话对象
        SqlSession session = SqlSessionUtil.getSession();
        //使用会话对象获取接口代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        //使用添加方法
        mapper.addUser(addUser);
        //获取添加后的字段的id 也就是最大id
        int id = mapper.getMaxById();
        //获取中间表对象
        UserRoleMapper userRoleMapper = session.getMapper(UserRoleMapper.class);
        //在中间表添加
        List<String> roleIds = addUser.getRoleIds();
        for (String roleId : roleIds) {
            userRoleMapper.add(id, Integer.parseInt(roleId));
        }
        //释放资源
        session.close();
    }

    @Override
    public void deleteUser(UpdateUser deleteUser) {
        //使用工具类获得会话对象
        SqlSession session = SqlSessionUtil.getSession();
        //获取中间表对象
        UserRoleMapper userRoleMapper = session.getMapper(UserRoleMapper.class);
        //在中间表删除
        userRoleMapper.deleteByUserId(deleteUser.getId());
        //使用会话对象获取接口代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        //使用删除方法
        mapper.deleteUser(deleteUser.getId());
        //释放资源
        session.close();
    }

}
